/**
 * Created by liuqingxia on 2016/7/28.
 */

var currRmMachineId;//要删除的机器记录id
var machineTable;

/**
 * main入口
 */
$(document).ready(function () {


   /* LTS.colFormatter.optFormat = function (v, row) {
        return generateOptButtons(row);
    }*/


    LTS.colFormatter.occupy = function (v, row) {
        return occupy(row);
    }

    LTS.colFormatter.disabled = function (v, row) {
        return disabled(row);
    }

    LTS.colFormatter.optFormat = function (v, row) {
        return optFormat(row);
    }

    machineTable = $("#machine-table-container").ltsTable({
        url: getUrlBySuffix("/yugong/machine/find"),
        templateId: 'machine-table'
    });

    machineTable.post({}, 1);

    clickRemoveBtn();
    clickConfirmRemoveBtn();
    clickUpdateBtn();
    clickConfirmUpdateBtn();

});


/**
 * 占用情况
 * @param row
 * @returns {*}
 */
function occupy(row) {
    var isOccupy = row['isOccupy'];
    if (isOccupy == 1) {
        return "是";
    } else if (isOccupy == 0) {
        return "否";
    } else {
        return "未知";
    }
}

/**
 * 是否已经失效
 * @param row
 * @returns {*}
 */
function disabled(row) {
    var isDisabled = row['isDisabled'];
    if (isDisabled == 1) {
        return "是";
    } else if (isDisabled == 0) {
        return "否";
    } else {
        return "未知";
    }
}

/**
 * 操作按钮
 * @param row
 */
function optFormat(row) {
    return "" + updateBtn + removeBtn;
}

/**
 * 点击删除按钮
 */
function clickRemoveBtn() {
    $(document).on("click", ".remove-btn", function () {
        currRmMachineId = getCurrentId($(this));
        // console.log("currRmMachineId: " + currRmMachineId);
        $("#remove-tip-modal").modal("show");
    });
}

/**
 * 点击确认删除按钮
 */
function clickConfirmRemoveBtn() {
    $(document).on("click", "#confirm-remove-btn", function () {
        $("#cancel-remove-btn").trigger("click");//提示框隐藏
        var params = {};
        params['id'] = $.trim(currRmMachineId);
        $.postHttpSync(params, getUrlBySuffix("/yugong/machine/remove"), function(json){
        	if (json && json.success) {
                swal({
    				title : "操作成功",
    				text : json['message'],
    				type : "success"
    			}, function() {
    				machineTable.post({}, 1);
    				$("#remove-tip-modal").modal("hide");
    			});
            } else {
                if (json) {
                    swal("失败", json['message'], 'error');
                }
            }
        });
    });
}

/**
 * 点击编辑按钮
 */
function clickUpdateBtn() {
    $(document).on("click", ".update-btn", function () {
        var id = getCurrentId($(this));
        // console.log(id);
        var ygManager = getObjectById(getUrlBySuffix("/yugong/machine/findById"), id);
        // console.log("ygManager: " + JSON.stringify(ygManager));
        fillTaskEditForm(ygManager);
        $("#machine-edit-modal").modal("show");//显示任务编辑模态框
    });
}

/**
 * 把对象的各属性赋值到编辑表格中
 * @param obj
 */
function fillTaskEditForm(obj) {
    $.each($('#editForm').parent().find(".form-control"), function () {

        var name = $.trim($(this).attr("name"));

        if (isNull(name)) {
            return true;
        }
        var value = $.trim(obj[name]);
        $(this).val(value);
    });
}

/**
 * 点击任务编辑模态框上的修改按钮
 */
function clickConfirmUpdateBtn() {
    $(document).on("click", "#edit-btn", function () {
        var params = getFormParams("editForm");
        // console.log("json:" + JSON.stringify(params));
        var isOccupy = $("#isOccupy").val();
        var isDisabled = $("#isDisabled").val();
        params['isOccupy'] = isOccupy;
        params['isDisabled'] = isDisabled;
        params['projectName'] = $("#projectSelected").val();
        $.postHttp(params, getUrlBySuffix("/yugong/machine/update"), function(json){
        	if (json && json.success) {
                swal({
    				title : "操作成功",
    				text : json['message'],
    				type : "success"
    			}, function() {
    				machineTable.post({}, pageNum);
    				$("#machine-edit-modal-dismiss").modal("hide");
    			});
            } else {
                if (json) {
                    swal("失败", json['message'], 'error');
                }
            }
        });
        
    });
}